angular.module('api.srv', [])

.service('srv', ['$http', 'config', function($http, config) {
    /*
     * 将json对象转化为参数字符串
     * @params {object} o
     * @string {string} a=1&b=2
     */
    function toParam(o) {
        var arr = [];
        for (var i in o) {
            arr.push(i + '=' + o[i]);
        }
        return arr.join('&');
    }

    /*
     * http请求
     * @params {object} options 请求参数
     *      {
     *      url: '',
     *      data: '',
     *      params: '',
     *      method: ''
     *      }
     */
    function ajax(options, cb) {
        if (typeof options == 'string') {
            options = {
                url: options
            }
        }
        if (!options.method) options.method = 'GET';
        if (options.method && /^(post|put|patch)$/.test(options.method.toLowerCase())) {
            options['headers'] = {
                'Content-Type': 'application/x-www-form-urlencoded'
            };
        }
        if (options.data && typeof options.data == 'object') {
            options.data = toParam(options.data);
        }
        if (!cb || typeof cb != 'function') {
            cb = config.noop;
        }
        options.url = config.host + options.url;
        $http(options).success(cb).error(function(data, status, headers, cfg) {
            if (!status) status = -1;
            cb({
                code: status,
                msg: '系统异常'
            });
        });
    }

var api = {};
/*
 * 基本接口
 */


/*
 * 测试接口 
 */
api.queryAll = function(cb) {
    ajax('users/queryAll', cb);
}

return api;

}])
